﻿using BlazorDownloadFile;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using HC.XAP.FileUpload.Module.BusinessObjects.FileUpload;
using Microsoft.Extensions.DependencyInjection;

namespace HC.XAP.FileUpload.Module.Controllers
{
    // For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx.
    public partial class DownloadFileController : ViewController
    {

        IBlazorDownloadFileService _blazorDownloadFileService;
        public void Inject(IBlazorDownloadFileService blazorDownloadFileService)
        {
            _blazorDownloadFileService = blazorDownloadFileService;
        }

        // Use CodeRush to create Controllers and Actions with a few keystrokes.
        // https://docs.devexpress.com/CodeRushForRoslyn/403133/
        public DownloadFileController()
        {
            InitializeComponent();
            TargetObjectType = typeof(HCOBFileUpload);
            // Target required Views (via the TargetXXX properties) and create their Actions.
            var downloadAction = new SimpleAction(this, "下载文件", PredefinedCategory.View);
            downloadAction.TargetViewType = ViewType.ListView;
            downloadAction.ImageName = "FillDown";
            downloadAction.Execute += DownloadAction_Execute;
           
        }

        private void DownloadAction_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            try
            {
                if (this.View.SelectedObjects == null || this.View.SelectedObjects.Count <= 0)
                {
                    Application.ShowViewStrategy.ShowMessage("请勾选列要下载的文件再下载.", InformationType.Error, 8000, InformationPosition.Top);
                    return;
                }

                foreach (HCOBFileUpload item in this.View.SelectedObjects)
                {
                    var fileName = item.SystemStoreFile.FileName;
                    using (var stream = new MemoryStream())
                    {
                        stream.Position = 0;
                        //item.SystemStoreFile.TempSourceStream.CopyToAsync(stream);
                        File.OpenRead(item.SystemStoreFile.RealFileName).CopyToAsync(stream);
                        //var byteArr = File.ReadAllBytes(zipFilePath);
                        _blazorDownloadFileService.DownloadFile(fileName, stream, "application/octet-stream");

                        // using var streamRef = new DotNetStreamReference(stream: stream);
                        //await jsRuntime.InvokeVoidAsync("downloadFileFromStream", fileName, streamRef);
                    }
                }
                Application.ShowViewStrategy.ShowMessage("您选择的文件已正在下载.", InformationType.Info, 5000, InformationPosition.Top);

                ObjectSpace.CommitChanges();
                ObjectSpace.Refresh();
                this.View.Refresh();
            }
            catch (Exception ex)
            {
                Application.ShowViewStrategy.ShowMessage($"下载出错:{ex.Message}", InformationType.Error, 8000, InformationPosition.Top);
            }
            
        }

        protected override void OnActivated()
        {
            base.OnActivated();
            // Perform various tasks depending on the target View.
            _blazorDownloadFileService = Application.ServiceProvider.GetService<IBlazorDownloadFileService>();
        }
        protected override void OnViewControlsCreated()
        {
            base.OnViewControlsCreated();
            // Access and customize the target View control.
        }
        protected override void OnDeactivated()
        {
            // Unsubscribe from previously subscribed events and release other references and resources.
            base.OnDeactivated();
        }
    }
}
